using System.Collections.Generic;
using UnityEngine;

namespace abilities.targets
{
    //符合半径或最接近单位的
    public class TargetCurrentOrNearest : TargetsAround
    {
        public override List<TargetObject> GetTargets(AbilityContext context)
        {
	        List<TargetObject> result = new();
            if (context.AbilityManager.Owner.isDead())
            {
                return result;
            }

            UnitObject  unit = context.AbilityManager.Owner;
            TargetObject currentTarget = new TargetObject(unit);// unit.getTarget();
            
            float distanceToCurrentTarget = Vector3.Distance(unit.transform.position, currentTarget.unit.transform.position);
            if(distanceToCurrentTarget < radius && distanceToCurrentTarget > minRadius)
            {
	            result.Add(currentTarget);
	            return result;
            }
            
            List<TargetObject > foundTargets = base.GetTargets(context);

            TargetObject  nearestTarget = null;
            float minCost = float.MaxValue;
            foreach(var  iter in foundTargets){
            	float distanceToTarget = Vector3.Distance(unit.transform.position, iter.unit.transform.position);
            	if(distanceToTarget < minCost){
            		minCost = distanceToTarget;
            		nearestTarget = iter;
            	}
            }
            result.Add(nearestTarget);
            return result;
        }
    }
}